package com.aplink.generic.xmlparser;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.res.Resources;
import android.text.TextUtils;

import com.aplink.generic.constant.FormDesignConstant;
import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.constant.type.BarcodeType;
import com.aplink.generic.constant.type.ControlType;
import com.aplink.generic.constant.type.EditTextType;
import com.aplink.generic.constant.type.ListViewSelection;
import com.aplink.generic.dialog.ApLinkProgressDialog;
import com.aplink.generic.pojo.ObjectFilter;
import com.aplink.generic.pojo.Value;
import com.aplink.generic.pojo.ValueList;
import com.aplink.generic.pojo.controlspec.BarcodeSpec;
import com.aplink.generic.pojo.controlspec.ButtonSpec;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.EditTextSpec;
import com.aplink.generic.pojo.controlspec.GallerySpec;
import com.aplink.generic.pojo.controlspec.GoogleMapsSpec;
import com.aplink.generic.pojo.controlspec.ImageViewSpec;
import com.aplink.generic.pojo.controlspec.ListviewSpec;
import com.aplink.generic.pojo.controlspec.RadioButtonSpec;
import com.aplink.generic.pojo.controlspec.actionbar.ActionBarSpec;
import com.aplink.generic.pojo.controlspec.gridview.GridViewItem;
import com.aplink.generic.pojo.controlspec.gridview.GridViewSpec;
import com.aplink.generic.pojo.controlspec.menu.PopupMenuSpec;
import com.aplink.generic.pojo.controlspec.tabhost.TabHostSpec;
import com.aplink.generic.pojo.controlspec.textview.TextViewSpec;
import com.aplink.generic.pojo.formdesign.FormDesign;
import com.aplink.generic.pojo.formdesign.InitValue;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.util.objectrule.EnableCondition;
import com.aplink.generic.util.objectrule.ObjectBaseRule;
import com.aplink.generic.util.objectrule.TargetCollection;
import com.aplink.generic.util.validation.Validation;
import com.aplink.generic.xmlparser.ctrlformat.CheckBoxFormat;
import com.aplink.generic.xmlparser.ctrlformat.DateTimeFormat;
import com.aplink.generic.xmlparser.ctrlformat.EditTextFormat;
import com.aplink.generic.xmlparser.ctrlformat.GalleryFormat;
import com.aplink.generic.xmlparser.ctrlformat.GoogleMapsFormat;
import com.aplink.generic.xmlparser.ctrlformat.ListViewFormat;
import com.aplink.generic.xmlparser.ctrlformat.SpinnerFormat;

public class FormDesignParser {

    private static final String CHECKBOX_FORMAT = "CheckBoxFormat";
    private static final String DATETIME_PICKER_FORMAT = "DateTimePickerFormat";
    private static final String EDITTEXT_FORMAT = "EditTextFormat";
    private static final String GALLERY_FORMAT = "GalleryFormat";
    private static final String GOOGLE_MAPS_FORMAT = "GoogleMapFormat";
    private static final String LISTVIEW_FORMAT = "ListViewFormat";
    public static final String NAME = "name";
    private static FormDesignParser sInstance = null;
    private static final String SPINNER_FORMAT = "SpinnerFormat";

    private static final String SWITCH_FORMAT = "SwitchFormat";

    public static FormDesignParser getInstance() {
        synchronized (FormDesignParser.class) {
            if (FormDesignParser.sInstance == null) {
                FormDesignParser.sInstance = new FormDesignParser();
            }
        }
        return FormDesignParser.sInstance;
    }

    protected String getViewTitile(final String viewTitle) {
        return ResourcesHelper.getStringByName(viewTitle);
    }

    public FormDesign parse(final int xmlFileID) {
        if (xmlFileID == ResourcesHelper.RESOURCE_NOT_FOUND) {
            return new FormDesign();
        }
        return parserIntoControls(xmlFileID, FormDesignConstant.FORM_DESIGN);
    }

    private GridViewSpec parseGalleryFormat(Node nodeFormat) {
        final GridViewSpec galleryFormat = new GridViewSpec();
        Element elementFormat = (Element) nodeFormat;
        NodeList nodeListFormat = elementFormat
                .getElementsByTagName(FormDesignConstant.LIST_FORMAT);
        nodeFormat = nodeListFormat.item(0);
        if (nodeFormat.getNodeType() == Node.ELEMENT_NODE) {
            if (nodeFormat.hasAttributes()) {
                final String dataObject = XMLPaserUtil.getAttrValue(nodeFormat,
                        FormDesignConstant.DATAOBJ);
                galleryFormat.setDataObject(dataObject);
            }

            elementFormat = (Element) nodeFormat;
            nodeListFormat = elementFormat
                    .getElementsByTagName(FormDesignConstant.LIST_FORMAT_COLUMN);
            for (int i = 0; i < nodeListFormat.getLength(); i++) {
                final Node nodeItem = nodeListFormat.item(i);
                final GridViewItem galleryItem = new GridViewItem();
                if (nodeItem.hasAttributes()) {
                    final String mappingName = XMLPaserUtil.getAttrValue(
                            nodeItem, FormDesignConstant.MAPPINGNAME);
                    galleryItem.setMappingName(mappingName);
                    final String dataObject = XMLPaserUtil.getAttrValue(
                            nodeItem, FormDesignConstant.DATAOBJ);
                    galleryItem.setDataObject(dataObject);
                    galleryFormat.add(galleryItem);
                }
            }

            nodeListFormat = elementFormat
                    .getElementsByTagName(FormDesignConstant.FILTER);
            final Node nodeFiler = nodeListFormat.item(0);
            if (nodeFiler.hasAttributes()) {
                final ObjectFilter filter = new ObjectFilter();
                final String mappingName = XMLPaserUtil.getAttrValue(nodeFiler,
                        FormDesignConstant.MAPPINGNAME);
                filter.setMappingName(mappingName);
                final String dataObject = XMLPaserUtil.getAttrValue(nodeFiler,
                        FormDesignConstant.DATAOBJ);
                filter.setDataObject(dataObject);
                final String defaultValue = XMLPaserUtil.getAttrValue(
                        nodeFiler, FormDesignConstant.DEFAULT_VALUE);
                filter.setDefaultValue(defaultValue);
                galleryFormat.setFilter(filter);
            }
        }
        return galleryFormat;
    }

    private FormDesign parserIntoControls(final int xmlfileId,
            final String tagname) {
        try {
            final Document doc = XMLPaserUtil.openDocumentFrom(xmlfileId);
            return parseXML(doc, tagname);
        } catch (final ParserConfigurationException e) {
            e.printStackTrace();
        } catch (final SAXException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        } catch (final Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return new FormDesign();
    }

    private ValueList parseValueList(final Node nodeValueList) {
        final Element element = (Element) nodeValueList;
        final ValueList valueList = new ValueList();
        if (nodeValueList.hasAttributes()) {
            final String dataObj = XMLPaserUtil.getAttrValue(nodeValueList,
                    FormDesignConstant.DATAOBJ);
            if (dataObj != null) {
                valueList.setDataObj(dataObj);
            }

            final String submitControl = XMLPaserUtil.getAttrValue(
                    nodeValueList, FormDesignConstant.SUBMIT_CONTROL);

            if (submitControl != null) {
                valueList.setSubmitControl(submitControl);
            }

            final String valueControl = XMLPaserUtil.getAttrValue(
                    nodeValueList, FormDesignConstant.VALUE_CONTROL);

            if (valueControl != null) {
                valueList.setValueControl(valueControl);
            }

            final String valueColumn = XMLPaserUtil.getAttrValue(nodeValueList,
                    FormDesignConstant.VALUE_COLUMN);
            if (valueColumn != null) {
                valueList.setValueColumn(valueColumn);
            }
        }
        final NodeList nodeList = element
                .getElementsByTagName(FormDesignConstant.VALUE);
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Value value = new Value();
            final Node valueItem = nodeList.item(i);
            if (valueItem.hasAttributes()) {
                final String mappingName = XMLPaserUtil.getAttrValue(valueItem,
                        FormDesignConstant.MAPPINGNAME);
                value.setMappingName(mappingName);
                final String nodeValue = XMLPaserUtil.getNodeValue(valueItem);
                value.setValue(nodeValue);
                valueList.put(nodeValue, value);
            }
        }
        return valueList;
    }

    private FormDesign parseXML(final Document doc, final String tagName) {
        final FormDesign formDesign = new FormDesign();
        final NodeList nList = doc.getElementsByTagName(tagName);
        if (nList.getLength() == 0) {
            return formDesign;
        }
        Node nNode = nList.item(0);
        Element element = (Element) nNode;
        // Parse Layout element
        NodeList nodes = element
                .getElementsByTagName(FormDesignConstant.LAYOUT);
        if (nodes.getLength() == 0) {
            return formDesign;
        }

        final Node node = nodes.item(0);
        if (node.hasAttributes()) {
            String value = XMLPaserUtil.getAttrValue(node,
                    FormDesignParser.NAME);
            if (!TextUtils.isEmpty(value)) {
                formDesign.setLayout(value);
            }
            value = XMLPaserUtil.getAttrValue(node,
                    FormDesignConstant.ANIMATION_IN);
            if (!TextUtils.isEmpty(value)) {
                formDesign.setAnimationIn(value);
            }

            value = XMLPaserUtil.getAttrValue(node,
                    FormDesignConstant.ANIMATION_OUT);
            if (!TextUtils.isEmpty(value)) {
                formDesign.setAnimationOut(value);
            }

            value = XMLPaserUtil.getAttrValue(node,
                    FormDesignConstant.ENABLE_NAVIGATE_DRAWER);
            if (!TextUtils.isEmpty(value)) {
                if (value.equalsIgnoreCase("N")) {
                    formDesign.setEnableNavigateDrawer(false);
                }
            }
        }

        nodes = element.getElementsByTagName(FormDesignConstant.SETTINGS);
        if (nodes.getLength() == 1) {
            if (nodes.item(0).hasAttributes()) {
                final String value = XMLPaserUtil.getAttrValue(nodes.item(0),
                        FormDesignConstant.IS_ALWAY_LOAD_FIRST_RECORD);
                if (!TextUtils.isEmpty(value) && value.equalsIgnoreCase("Y")) {
                    formDesign.setAlwayLoadFirstRecord(true);
                }
            }
        }
        // Parse Option menu
        final ActionBarSpec optionMenuSpec = ActionBarParser.getInstance()
                .parse(element);
        formDesign.setActionBar(optionMenuSpec);
        // Parse Progress dialog
        final ApLinkProgressDialog apLinkProgressDialog = DialogParser
                .parseApLinkProgressDialog(element);
        formDesign.setApLinkProgressDialog(apLinkProgressDialog);

        // Parse AlerDialog
        formDesign.setAlertDialog(DialogParser.parseApLinkAlertDialog(element));
        formDesign.setObjectLoadBy(SelectWhereParser.parsse(element));
        final InitValue initValue = InitValueParser.parseInitValue(element);
        formDesign.setInitValue(initValue);
        // Parse ctlspecs element
        nodes = element.getElementsByTagName(FormDesignConstant.SPECS);
        if (nodes.getLength() == 0) {
            return formDesign;
        }
        nNode = nodes.item(0);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            nodes = element.getElementsByTagName(FormDesignConstant.STYLE);
            if (nodes.getLength() == 0) {
                return formDesign;
            }

            for (int temp = 0; temp < nodes.getLength(); temp++) {
                NodeList subNodeList = null;
                ControlSpec controlSpec = null;
                final Node nControlNode = nodes.item(temp);
                String controlType = null;
                String controlId = null;
                boolean isSearch = false;
                String searchControl = null;
                String refreshControl = null;
                String refreshValue = null;
                String searchFields = null;
                String mappingName = null;
                boolean isToggleOfflineMode = false;
                String dataInputType = null;
                boolean isVisibility = true;
                int minLength = EditTextSpec.MIN_LENGTH;
                int maxLenght = EditTextSpec.MAX_LENGTH;
                String dataObject = null;
                boolean isChecked = false;
                String valueProperty = null;
                String contentData = null;
                String rptrDesign = null;
                String rowItem = null;
                boolean isReadOnly = false;
                String validationGroup = null;
                final ObjectBaseRule objectBaseRule = null;
                String value = null;
                int tabhostStartPageIndex = 0;
                String tabhostBackgroundColor = null;
                String tabhostDisplayStyle = null;
                boolean isLoadDataByObj = true;
                String ctrlResult = null;
                String imagePath = null;
                int barcodeType = 0;
                boolean enableFlashLight = false;
                String header = null;
                String footer = null;
                String activeControl = null;
                String regex = null;
                String selectWhere = null;
                boolean isLoadAll = false;
                String displayType = null;
                String displayProperty = null;
                boolean isAutoRefreshValue = false;
                ValueList valueList = null;
                TargetCollection targetCollection = null;
                Validation validation = null;
                ActionType actionType = null;
                ListViewSelection listViewSelection = null;
                String linkingValueProp = null;
                boolean isToggleAutoSync = false;
                EnableCondition enableCondition = null;
                String displayFormat = null;
                String animation = null;
                if (nControlNode.hasAttributes()) {
                    // find attr ctltype
                    controlType = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.TYPE);
                    controlId = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.ID);
                    value = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.VALUE);
                    isSearch = false;
                    if (XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.ISSEARCH) != null) {
                        final String val = XMLPaserUtil.getAttrValue(
                                nControlNode, FormDesignConstant.ISSEARCH);
                        if (val.compareToIgnoreCase("Y") == 0) {
                            isSearch = true;
                        }
                    }
                    final String barType = XMLPaserUtil.getAttrValue(
                            nControlNode, FormDesignConstant.BARCODE_TYPE);
                    barcodeType = BarcodeType.get(barType);
                    final String flash = XMLPaserUtil
                            .getAttrValue(nControlNode,
                                    FormDesignConstant.ENABLE_FLASH_LIGHT);
                    if ((flash != null) && flash.equalsIgnoreCase("Y")) {
                        enableFlashLight = true;
                    }

                    value = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.SELECTION_MODE);
                    if (!TextUtils.isEmpty(value)) {
                        listViewSelection = ListViewSelection.get(value);
                    }

                    displayFormat = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.DISPLAY_FORMAT);
                    displayProperty = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.DISPLAYPROP);

                    searchControl = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.SEARCHCTRL);
                    activeControl = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.CTRL_ACTIVE);
                    ctrlResult = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.CTRL_RESULT);
                    imagePath = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.IMAGE_PATH);
                    // find searchFields
                    searchFields = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.SEARCHFIELDS);
                    // find mapping name
                    mappingName = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.MAPPINGNAME);
                    // find inputType
                    dataInputType = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.INPUTTYPE);

                    // find inputType
                    value = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.IS_TOGGLE_OFFLINE_MODE);
                    if (!TextUtils.isEmpty(value)
                            && value.equalsIgnoreCase("Y")) {
                        isToggleOfflineMode = true;
                    }

                    // find inputType
                    value = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.IS_TOGGLE_AUTO_SYNC);
                    if (!TextUtils.isEmpty(value)
                            && value.equalsIgnoreCase("Y")) {
                        isToggleAutoSync = true;
                    }

                    // find Visibility Type
                    if (XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.VISIBILITY) != null) {
                        final String val = XMLPaserUtil.getAttrValue(
                                nControlNode, FormDesignConstant.VISIBILITY);
                        if (val.compareToIgnoreCase("N") == 0) {
                            isVisibility = false;
                        }
                    }
                    displayType = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.DISPLAY_TYPE);
                    // find minLength
                    minLength = EditTextSpec.MIN_LENGTH;
                    if (XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.MINLENGTH) != null) {
                        final String val = XMLPaserUtil.getAttrValue(
                                nControlNode, FormDesignConstant.MINLENGTH);
                        try {
                            minLength = Integer.parseInt(val);
                        } catch (final NumberFormatException e) {
                            minLength = EditTextSpec.MIN_LENGTH;
                        }
                    }
                    // find maxLength
                    maxLenght = EditTextSpec.MAX_LENGTH;
                    if (XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.MAXLENGTH) != null) {
                        final String val = XMLPaserUtil.getAttrValue(
                                nControlNode, FormDesignConstant.MAXLENGTH);
                        try {
                            maxLenght = Integer.parseInt(val);
                        } catch (final NumberFormatException e) {
                            maxLenght = EditTextSpec.MAX_LENGTH;
                        }
                    }
                    // find dataobj
                    dataObject = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.DATAOBJ);

                    // find IsChecked
                    if (XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.ISCHECKED) != null) {
                        final String val = XMLPaserUtil.getAttrValue(
                                nControlNode, FormDesignConstant.ISCHECKED);
                        if (val.equalsIgnoreCase("Y")) {
                            isChecked = true;
                        }
                    }

                    // find IsChecked
                    if (XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.OBJ_LOAD_BY_CONFIG) != null) {
                        final String val = XMLPaserUtil.getAttrValue(
                                nControlNode,
                                FormDesignConstant.OBJ_LOAD_BY_CONFIG);
                        if (val.equalsIgnoreCase("N")) {
                            isLoadDataByObj = false;
                        }
                    }

                    final String isAuto = XMLPaserUtil
                            .getAttrValue(nControlNode,
                                    FormDesignConstant.AUTO_REFRESH_VALUE);
                    if ((isAuto != null) && isAuto.equalsIgnoreCase("Y")) {
                        isAutoRefreshValue = true;
                    }
                    // find ValueProp
                    valueProperty = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.VALUEPROP);
                    selectWhere = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.FILTER_BY);
                    // find ContentData
                    contentData = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.CONTENTDATA);
                    // find rptrDesign
                    rptrDesign = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.RPTRDESIGN);
                    // find rowItem
                    rowItem = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.ROWITEM);
                    header = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.HEADER);
                    footer = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.FOOTER);
                    // find regular expression
                    regex = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.REGULAR_EXPRESSION);

                    // refresh control;
                    refreshControl = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.REFRESH_CONTROL);
                    refreshValue = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.REFRESH_VALUE);

                    animation = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.ANIMATION);
                    // Control action;

                    final String action = XMLPaserUtil.getAttrValue(
                            nControlNode, FormDesignConstant.ACTION);
                    if (!TextUtils.isEmpty(action)) {
                        actionType = ActionType.get(action);
                    }

                    if (XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.CURRENT_PAGE_INDEX) != null) {
                        tabhostStartPageIndex = Integer.parseInt(XMLPaserUtil
                                .getAttrValue(nControlNode,
                                        FormDesignConstant.CURRENT_PAGE_INDEX));
                    }
                    tabhostBackgroundColor = XMLPaserUtil.getAttrValue(
                            nControlNode, FormDesignConstant.BACKGROUND_COLOR);

                    tabhostDisplayStyle = XMLPaserUtil.getAttrValue(
                            nControlNode, FormDesignConstant.DISPLAY);

                    linkingValueProp = XMLPaserUtil.getAttrValue(nControlNode,
                            FormDesignConstant.LINKING_VALUE_PROP);

                    final String loadAll = XMLPaserUtil.getAttrValue(
                            nControlNode, FormDesignConstant.LOAD_ALL);
                    if (!TextUtils.isEmpty(loadAll)
                            && loadAll.equalsIgnoreCase("Y")) {
                        isLoadAll = true;
                    }
                }

                if (nControlNode.getNodeType() == Node.ELEMENT_NODE) {
                    final Element elementControlNode = (Element) nControlNode;
                    // set ReadOnly
                    final NodeList nodeReadOnly = elementControlNode
                            .getElementsByTagName(FormDesignConstant.ISREADONLY);
                    if (nodeReadOnly.getLength() != 0) {
                        isReadOnly = true;
                    }

                    // set validationGroup
                    final NodeList nodeValidationGroup = elementControlNode
                            .getElementsByTagName(FormDesignConstant.VALIDATIONGROUP);
                    if (nodeValidationGroup.getLength() != 0) {
                        validationGroup = XMLPaserUtil
                                .getNodeValue(nodeValidationGroup.item(0));
                    }

                    // find targets tag for ListView
                    targetCollection = TargetCollectionPaser
                            .parse(elementControlNode);
                    // Parse Validation
                    validation = ValidationParser
                            .validationParser(elementControlNode);

                    enableCondition = EnableConditionParser
                            .parse(elementControlNode);
                    final NodeList nodeValueList = elementControlNode
                            .getElementsByTagName(FormDesignConstant.VALUE_LIST);
                    if (nodeValueList.getLength() != 0) {
                        valueList = parseValueList(nodeValueList.item(0));
                    }
                }
                final ControlType type = ControlType.get(controlType);
                element = (Element) nControlNode;
                if (type.equals(ControlType.GALLERY)) {
                    formDesign.setHasGallery(true);
                    formDesign.setGalleryID(controlId);
                    subNodeList = element
                            .getElementsByTagName(FormDesignParser.GALLERY_FORMAT);
                    if (subNodeList.getLength() == 1) {
                        controlSpec = GalleryFormat.parse(subNodeList.item(0));
                    } else {
                        final GallerySpec gallerySpec = GalleryPaser
                                .parse(nControlNode);
                        gallerySpec.setContentData(contentData);
                        gallerySpec.setImagePath(imagePath);
                        gallerySpec.setReadOnly(isReadOnly);
                        controlSpec = gallerySpec;
                    }
                } else if (type.equals(ControlType.GRIDVIEW)) {
                    final GridViewSpec galleryFormat = parseGalleryFormat(nControlNode);
                    galleryFormat.setContentData(contentData);
                    galleryFormat.setDataObject(dataObject);
                    galleryFormat.setRowItem(rowItem);
                    galleryFormat.setRptrdesign(rptrDesign);
                    controlSpec = galleryFormat;
                } else if (type.equals(ControlType.VIEWPAGER)) {
                    controlSpec = ViewPagerParser.parse(nControlNode);
                } else if (type.equals(ControlType.TABHOST)) {
                    final TabHostSpec tabHostFormat = new TabHostSpec();
                    tabHostFormat.setPageCollection(PageCollectionParser
                            .parse(nControlNode));
                    tabHostFormat.setBackgroundColor(tabhostBackgroundColor);
                    tabHostFormat.setDisplay(tabhostDisplayStyle);
                    tabHostFormat.setStartTab(tabhostStartPageIndex);
                    controlSpec = tabHostFormat;
                } else if (type.equals(ControlType.EDITTEXT)) {
                    subNodeList = element
                            .getElementsByTagName(FormDesignParser.EDITTEXT_FORMAT);
                    if (subNodeList.getLength() == 1) {
                        final EditTextSpec editTextSpec = EditTextFormat
                                .parse(subNodeList.item(0));
                        editTextSpec.setValidation(validation);
                        controlSpec = editTextSpec;
                    } else {
                        controlSpec = new EditTextSpec(isReadOnly, isSearch,
                                validation, EditTextType.get(dataInputType),
                                searchControl, searchFields, minLength,
                                maxLenght, regex);
                    }
                } else if (type.equals(ControlType.LISTVIEW)) {
                    formDesign.setHasListView(true);
                    formDesign.setListViewID(controlId);
                    subNodeList = element
                            .getElementsByTagName(FormDesignParser.LISTVIEW_FORMAT);
                    if (subNodeList.getLength() == 1) {
                        controlSpec = ListViewFormat.parse(subNodeList.item(0));
                        ((ListviewSpec) controlSpec)
                                .setTargetCollection(targetCollection);
                    } else {
                        controlSpec = new ListviewSpec(rowItem, rptrDesign,
                                contentData, targetCollection, header, footer,
                                listViewSelection);
                        ((ListviewSpec) controlSpec).setLoadAll(isLoadAll);
                        ((ListviewSpec) controlSpec)
                                .setLinkingValueProp(linkingValueProp);
                        ((ListviewSpec) controlSpec)
                                .setDisplayProperty(displayProperty);
                    }
                } else if (type.equals(ControlType.SPINNER)) {
                    subNodeList = element
                            .getElementsByTagName(FormDesignParser.SPINNER_FORMAT);
                    if (subNodeList.getLength() == 1) {
                        controlSpec = SpinnerFormat.parse(subNodeList.item(0));
                    } else {
                        controlSpec = SpinnerParser.parse(nControlNode);
                        controlSpec.setValueList(valueList);
                    }
                } else if (type.equals(ControlType.BUTTON)) {
                    final ButtonSpec buttonSpec = new ButtonSpec(
                            targetCollection);
                    buttonSpec.setValidation(validation);
                    controlSpec = buttonSpec;
                } else if (type.equals(ControlType.CHECKBOX)) {
                    subNodeList = element
                            .getElementsByTagName(FormDesignParser.CHECKBOX_FORMAT);
                    if (subNodeList.getLength() == 1) {
                        controlSpec = CheckBoxFormat.parse(subNodeList.item(0));
                    } else {
                        controlSpec = CheckBoxParser.parse(nControlNode);
                    }
                } else if (type.equals(ControlType.BARCODE_SCANNER)) {
                    controlSpec = new BarcodeSpec(ctrlResult, imagePath,
                            enableFlashLight, barcodeType);
                } else if (type.equals(ControlType.POPUP_MENU)) {
                    final PopupMenuSpec popupMenuSpec = PopupMenuParser
                            .parsePopupMenuFormat(nControlNode);
                    popupMenuSpec.setActiveControl(activeControl);
                    controlSpec = popupMenuSpec;
                } else if (type.equals(ControlType.RADIO_BUTTON)) {
                    controlSpec = new RadioButtonSpec(isChecked, value);
                } else if (type.equals(ControlType.TEXTVIEW)) {
                    controlSpec = new TextViewSpec(contentData);
                } else if (type.equals(ControlType.DATETIME_PICKER)) {
                    subNodeList = element
                            .getElementsByTagName(FormDesignParser.DATETIME_PICKER_FORMAT);
                    if (subNodeList.getLength() == 1) {
                        controlSpec = DateTimeFormat.parse(subNodeList.item(0));
                    }
                } else if (type.equals(ControlType.IMAGEVIEW)) {
                    controlSpec = new ImageViewSpec(isReadOnly);
                } else if (type.equals(ControlType.SWITCH)) {
                    subNodeList = element
                            .getElementsByTagName(FormDesignParser.SWITCH_FORMAT);
                    if (subNodeList.getLength() == 1) {
                        controlSpec = SwitchParser.parse(subNodeList.item(0));
                    } else {
                        controlSpec = SwitchParser.parse(nControlNode);
                    }
                } else if (type.equals(ControlType.GOOGLE_MAPS)) {
                    subNodeList = element
                            .getElementsByTagName(FormDesignParser.GOOGLE_MAPS_FORMAT);
                    if (subNodeList.getLength() == 1) {
                        controlSpec = GoogleMapsFormat.parse(subNodeList
                                .item(0));
                    } else {
                        controlSpec = new GoogleMapsSpec();
                    }
                }

                controlSpec.setFilterBy(selectWhere);
                controlSpec.setLoadByConfig(isLoadDataByObj);
                controlSpec.setRefreshControl(refreshControl);
                controlSpec.setRefreshValue(refreshValue);
                controlSpec.setValueProp(valueProperty);
                controlSpec.setDisplayType(displayType);
                controlSpec.setControlId(controlId);
                controlSpec.setControlType(controlType);
                controlSpec.setDataObject(dataObject);
                controlSpec.setMappingName(mappingName);
                controlSpec.setValidationGroup(validationGroup);
                controlSpec.setValue(value);
                controlSpec.setVisibility(isVisibility);
                controlSpec.setObjectBaseRule(objectBaseRule);
                controlSpec.setAutoRefreshValue(isAutoRefreshValue);
                controlSpec.setToggleOfflineMode(isToggleOfflineMode);
                controlSpec.setToggleAutoSync(isToggleAutoSync);
                controlSpec.setActionType(actionType);
                controlSpec.setEnableCondition(enableCondition);
                controlSpec.setDisplayFormat(displayFormat);
                controlSpec.setAnimation(animation);
                if (!TextUtils.isEmpty(controlSpec.getIDName())) {
                    formDesign.put(controlSpec.getIDName(), controlSpec);
                }
            }
        }
        return formDesign;
    }
}
